package com.loan.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loan.common.constant.Constants;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.enums.PayCompanyEnum;
import com.loan.common.pay.hqpay.HQPayUtils;
import com.loan.common.utils.StringUtils;
import com.loan.common.utils.redis.RedisUtils;
import com.loan.system.domain.AddressEntity;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.BankEntity;
import com.loan.system.domain.PayConfigEntity;
import com.loan.system.domain.bo.BankVo;
import com.loan.system.mapper.BankMapper;
import com.loan.system.mapper.PayConfigMapper;
import com.loan.system.service.BankService;
import com.loan.system.service.PayConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author shiguang on 2022/9/14 14:47
 */
@Service
@RequiredArgsConstructor
public class BankServiceImpl implements BankService {

    private final BankMapper bankMapper;

    private final PayConfigMapper payConfigMapper;

    @Override
    public BankEntity selectByBankNo(String bankNo) {
        return bankMapper.selectVoOne(new QueryWrapper<BankEntity>().eq("bank_no", bankNo));
    }

    @Override
    public void insertBank(BankEntity bankEntity) {
        bankMapper.insert(bankEntity);
    }

    @Override
    public void updateBank(BankEntity bankEntity) {
        bankMapper.updateById(bankEntity);
    }

    @Override
    public TableDataInfo<BankEntity> selectPageBankList(BankEntity entity, PageQuery pageQuery) {
        Page<BankEntity> page = bankMapper.selectPageApplyList(pageQuery.build(), this.buildQueryWrapper(entity));
        return TableDataInfo.build(page);
    }

    @Override
    public void updateHqpayBankId() {
        List<PayConfigEntity> list = payConfigMapper.selectList(new QueryWrapper<PayConfigEntity>().eq("pay_company", PayCompanyEnum.HQ_PAY));
        for (PayConfigEntity entity : list) {
            String result = HQPayUtils.listBank(entity.getHqpayToken());
            if (StringUtils.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                Boolean flag = jsonObject.getBoolean("success");
                if (flag) {
                    JSONArray banks = jsonObject.getJSONObject("data").getJSONArray("banks");
                    if (CollectionUtil.isNotEmpty(banks)) {
                        for (int i = 0; i < banks.size(); i++) {
                            String bankId = banks.getJSONObject(i).getString("bankId");
                            String bankName = banks.getJSONObject(i).getString("bankName");
                            if (StringUtils.isNotBlank(bankName)) {
                                BankEntity bankEntity = bankMapper.selectOne(new QueryWrapper<BankEntity>().eq("simple_name", bankName));
                                if (ObjectUtil.isNotNull(bankEntity)) {
                                    bankEntity.setHqpayBankId(bankId);
                                    bankMapper.updateById(bankEntity);
                                }else {
                                    bankEntity = new BankEntity();
                                    bankEntity.setHqpayBankId(bankId);
                                    bankEntity.setSimpleName(bankName);
                                    bankMapper.insert(bankEntity);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public BankEntity selectById(Long bankId) {
        return bankMapper.selectById(bankId);
    }

    @Override
    public List<BankEntity> list() {
        return bankMapper.selectList(new QueryWrapper<BankEntity>().orderByAsc("simple_name"));
    }

    private Wrapper<BankEntity> buildQueryWrapper(BankEntity entity) {
        Map<String, Object> params = entity.getParams();
        QueryWrapper<BankEntity> wrapper = Wrappers.query();
        return wrapper;
    }
}
